package com.gdut.simple.view.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.gdut.simple.R;
import com.gdut.simple.common.LogUtils;
import com.gdut.simple.view.adapter.base.AbsRecyclerAdapter;
import com.gdut.simple.view.adapter.base.AbsViewHolder;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.HashMap;
import java.util.List;

public class PhotoPickAdapter extends AbsRecyclerAdapter<String> {

    private Object tag;
    private int mImageWidth;
    private OnItemSelectedListener onItemSelectedListener;

    private HashMap<String, SparseBooleanArray> mFolderSelectedMap = new HashMap<>();
    private String mCurrentFolder;
    private SparseBooleanArray mSelectedMap;

    public PhotoPickAdapter(Context context, String currentFolder, List<String> list) {
        super(context, list);
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        mImageWidth = metrics.widthPixels / 3;
        mCurrentFolder = currentFolder;
        mSelectedMap = new SparseBooleanArray();
        initArray(mSelectedMap, list);
        mFolderSelectedMap.put(mCurrentFolder, mSelectedMap);
    }

    @Override
    protected AbsViewHolder createHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolder(mInflater.inflate(R.layout.layout_pick_image_item, parent, false));
    }

    @Override
    protected void showViewHolder(AbsViewHolder holder, final int position) {
        mSelectedMap = mFolderSelectedMap.get(mCurrentFolder);
        final ItemViewHolder viewHolder = (ItemViewHolder) holder;
        Picasso.with(mContext)
                .load(new File(mData.get(position)))
                .placeholder(R.drawable.ic_place_holder)
                .error(R.drawable.ic_load_error)
                .config(Bitmap.Config.RGB_565)
                .resize(mImageWidth, mImageWidth)
                .centerCrop()
                .tag(tag = mData.get(position))
                .into(viewHolder.image);
        viewHolder.select.setOnCheckedChangeListener(null);
        viewHolder.select.setChecked(mSelectedMap.get(position));
        viewHolder.select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                mSelectedMap.put(position, b);
                if (b) {
                    if (onItemSelectedListener != null) {
                        onItemSelectedListener.onChecked(compoundButton, mData.get(position));
                    }
                } else {
                    if (onItemSelectedListener != null) {
                        onItemSelectedListener.onRemoved(mData.get(position));
                    }
                }
            }
        });
    }

    public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
        this.onItemSelectedListener = onItemSelectedListener;
    }

    public interface OnItemSelectedListener {

        void onChecked(CompoundButton compoundButton, String image);

        void onRemoved(String image);

    }

    public Object getTag() {
        return tag;
    }

    public void setCurrentFolder(String folder, List<String> data) {
        LogUtils.e("PickAdapter", "current folder" + folder);
        if (!mFolderSelectedMap.containsKey(folder)) {
            SparseBooleanArray array = new SparseBooleanArray();
            initArray(array, data);
            mFolderSelectedMap.put(folder, array);
        }
        mCurrentFolder = folder;
        mSelectedMap = mFolderSelectedMap.get(mCurrentFolder);
        mData.clear();
        mData.addAll(data);
        notifyDataSetChanged();
    }

    private void initArray(SparseBooleanArray array, List<String> data) {
        for (int i = 0; i < data.size(); i++) {
            array.put(i, false);
        }
    }

    private static class ItemViewHolder extends AbsViewHolder {

        ImageView image;
        CheckBox select;

        ItemViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.id_pick_image);
            select = (CheckBox) itemView.findViewById(R.id.id_select_image);
        }
    }

}
